Conditions | 28 |
Paths | 25 |
Total Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like isTrue.js ➔ isTrue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | |||
2 | export default function isTrue(v1, operator, v2) { |
||
3 | var eval1 |
||
4 | var eval2 |
||
5 | switch (operator) { |
||
6 | case '!=': |
||
7 | return (v1 != v2) |
||
8 | case '!==': |
||
9 | return (v1 !== v2) |
||
10 | case '==': |
||
11 | return (v1 == v2) |
||
12 | case '===': |
||
13 | return (v1 === v2) |
||
14 | case '<': |
||
15 | return (v1 < v2) |
||
16 | case '<=': |
||
17 | return (v1 <= v2) |
||
18 | case '>': |
||
19 | return (v1 > v2) |
||
20 | case '>=': |
||
21 | return (v1 >= v2) |
||
22 | case '&&': |
||
23 | eval1 = false |
||
24 | eval2 = false |
||
25 | if((!!v1 === true && !Array.isArray(v1))|| (Array.isArray(v1) && v1.length>0)) eval1 = true |
||
|
|||
26 | if((!!v2 === true && !Array.isArray(v2))|| (Array.isArray(v2) && v2.length>0)) eval2 = true |
||
27 | |||
28 | return (eval1 && eval2) |
||
29 | case '||': |
||
30 | eval1 = false |
||
31 | eval2 = false |
||
32 | if((!!v1 === true && !Array.isArray(v1))|| (Array.isArray(v1) && v1.length>0)) eval1 = true |
||
33 | if((!!v2 === true && !Array.isArray(v2))|| (Array.isArray(v2) && v2.length>0)) eval2 = true |
||
34 | |||
35 | return (eval1 || eval2) |
||
36 | default: |
||
37 | return false |
||
38 | } |
||
39 | } |
||
40 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.